import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/components/home/Home'
import Login from '@/components/login/Login'
import Users from '@/components/users/Users'
import Roles from '@/components/roles/Roles'
import Rights from '@/components/rights/Rights'
import Categories from '@/components/categories/Categories'
import Goods from '@/components/goods/Goods'
import GoodsAdd from '@/components/goods-add/GoodsAdd'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// 引入自己的css
import '@/assets/css/index.css'
Vue.use(ElementUI)
Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    {path: '/login', component: Login},
    {
      path: '/home',
      component: Home,
      // 配置子路由
      children: [
        {path: '/users', component: Users},
        {path: '/roles', component: Roles},
        {path: '/rights', component: Rights},
        {path: '/categories', component: Categories},
        {path: '/goods', component: Goods},
        {path: '/goods-add', component: GoodsAdd}
      ]
    }
  ]
})

// 导航守卫进行登陆拦截
router.beforeEach((to, from, next) => {
  // ...
  if (to.path === '/login') {
    return next()
  }
  const token = localStorage.getItem('token')
  if (!token) {
    return next('/login')
  }
  next()
})
export default router
